/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package games;

import minmax.MinMax;
import java.util.ArrayList;
import java.lang.Integer;
import games.FourInARowState.FourInARowMove;
import java.util.Iterator;
import minmax.GameOutcome;
import minmax.GameState;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Mikael
 */
public class FourInARowNullMoveTest {
    
    @Test
    public void testMoves_whenXIsAboutToWin() {
        FourInARowState state = SetupGameState();
        
        Iterator<FourInARowMove> result = state.moves();
        ArrayList<FourInARowMove> moves = new ArrayList<FourInARowMove>();
        while(result.hasNext()){
            FourInARowMove step = result.next();
            moves.add(step);
            System.out.println(step.toString());
        }
        
        assertEquals(7, moves.size());
        int[] bits = new int[]{28,8,16,17,4,5,6};
        for (int i = 0; i < moves.size(); i++) {
            
            assertEquals(moves.get(i).getBit(), bits[i]);
        }
        //assertEquals(0, moves.get(0).);
    }
    
    @Test
    public void testEvaluation_whenHasToMakeACertainMoveToNotLose() {
        FourInARowState state = SetupGameState();
        MinMax minmax = new MinMax(10);
        
        Iterator<FourInARowMove> result = state.moves();
        ArrayList<FourInARowMove> moves = new ArrayList<FourInARowMove>();
        while(result.hasNext()){
            FourInARowMove step = result.next();
            moves.add(step);
            System.out.println(step.toString());
        }
        
        assertEquals(7, moves.size());
        int min = Integer.MIN_VALUE;
        int[] values = new int[]{min,min,min,0,min,min,min};
        for (int i = 0; i < moves.size(); i++) {
            
            assertEquals(values[i], minmax.maxMove(state, moves.get(i), min, Integer.MAX_VALUE));
        }
        //assertEquals(0, moves.get(0).);
    }

    private FourInARowState SetupGameState() {
        /*
         | | | | | |
         | | | | | |
        X| | | | | |
        0| | | | | |
        0| |X|X| | |
        0|0|X|X| | |
         */
        FourInARowState state = new FourInARowState();
        state.move(new FourInARowMove(false, 3, 0));
        state.move(new FourInARowMove(true, 0, 0));
        state.move(new FourInARowMove(false, 2, 0));
        state.move(new FourInARowMove(true, 1, 0));
        state.move(new FourInARowMove(false, 2, 1));
        state.move(new FourInARowMove(true, 0, 1));
        state.move(new FourInARowMove(false, 3, 1));
        state.move(new FourInARowMove(true, 0, 2));
        state.move(new FourInARowMove(false, 0, 3));
        return state;
    }
}
